/***
 * author: Adam Pociejowski
 */

package requests.music;

import java.util.ArrayList;
import java.util.Random;

import requests.FileFinder;
import requests.IRequest;
import requests.settings.Settings;
import main.DemiGui;

public class Mp3Random implements IRequest {
	
	private DemiGui instance;
	private FileFinder finder;
	private String path;
	private String[] songs;
	private Random los = new Random();
	private int songCounter = 0, max, limit = 10;
	public int off = 0;
	private Mp3PlaylistQueue queue;
	private Mp3Player player;
	
	public Mp3Random(DemiGui instance, FileFinder finder, String path, Settings settings) {
		
		this.instance = instance;
		this.path = path;
		this.finder = finder;
		queue = new Mp3PlaylistQueue(500, 450, limit, this, settings); 
		
	}
	
	public void createPlaylist() {
		
	/*********     Creating random playlist with songs from mp3 browser     *********/
		
		off = 0;
		String list[] = finder.Find(path, "");
		max = list.length;
		ArrayList<String> titleList = new ArrayList<String>(list.length);
		
		for (int i = 0; i < list.length; i++) {
			titleList.add(list[i]);
		}
		
		songs = new String[list.length];
		int counter = list.length;
		
		for (int i = 0; i < list.length; i++) {
			int index = los.nextInt(counter);
			songs[i] = titleList.get(index);
			titleList.remove(index);
			counter--;
		}
		next();	
		
	/**********************************************************************************/
		
	}
	
	public void next() {
		
		/**** Creating new instance of Class Mp3Player (starting playing new song) ****/
		
		if (songCounter < max) {
			String tmp = path;
			tmp += songs[songCounter];
			player = new Mp3Player(tmp, instance, this, songs[songCounter]);
			String playlistSongs[] = playlistCut();
			queue.paint(playlistSongs);
			songCounter++;
		}
		else {
			returnDEMI("End of Playlist");
		}
		
		/*******************************************************************************/
		
	}
	
	
	public void nextOnClick(String title) {
		
	/*******   After clicked on JButton in Mp3PlaylistQueue class delegate here    *******/	
	
		for (int i = songCounter; i < songCounter + limit; i++) {
			if (title.equals(songs[i])) {
				songCounter = i;
				break;
			}
		}
		player.exit();
		player = null;
		next();
		
	/*************************************************************************************/	
		
	}
	
	@SuppressWarnings("deprecation")
	public void stop() {
		player.isPlaylist = false;
		player.singleExit();
		queue.frame.setVisible(false);
		off = 1;
		returnDEMI("Mp3 Random - Playlist terminated..");
		instance.clear();
		Thread.currentThread().stop();
	}
		
	
	public String[] playlistCut() {	
		
	/*********  Cuting from playlist 10 next songs to whow in GUI               **********/
		
		int limit;
		
		if (songs.length - songCounter > 10) 
			limit = 10;
		else 
			limit = songs.length - songCounter;

		String playlist[] = new String[limit];
		int k = 0;
		for (int i = songCounter; i < songCounter + limit; i++) {
			playlist[k] = songs[i];
			k++;
		}
		return playlist;
		
	/***********************************************************************************/
		
	}
	

	@Override
	public void returnDEMI(String text) {
		instance.Monitor(text);
	}

}
